package com.hzx.service.impl;

import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hzx.dao.DeptDao;
import com.hzx.dao.EmpDao;
import com.hzx.entity.Dept;
import com.hzx.entity.Emp;
import com.hzx.entity.PageBean;
import com.hzx.service.DeptService;
import com.hzx.service.EmpService;

@Service
@Transactional
public class DeptServiceImpl implements DeptService {
	@Autowired
	private DeptDao deptDao;

	public void save(Dept model) {
		deptDao.save(model);
	}

	@Override
	public void pageQuery(PageBean pageBean) {
		deptDao.pageQuery(pageBean);
	}

	@Override
	public Dept findById(Integer did) {
		return deptDao.findById(did);
	}

	@Override
	public void saveOrupdate(Dept model) {
		deptDao.saveOrupdate(model);
	}

	@Override
	public List<String> checkDelete(Integer did) {
		Dept dept = deptDao.findById(did);
		return null;
	}

	@Override
	public void deleteDept(Integer did) {
		Dept dept = deptDao.findById(did);
		dept.setDis(0);
		//该部门下所有员工
		Set<Emp> emps = dept.getEmps();
		// 离职所以员工
		for (Emp emp : emps) {
			emp.setDept(null);
			emp.setEis(0);
		}
		deptDao.update(dept);
	}

	@Override
	public void restoreDept(Integer did) {
		Dept dept = deptDao.findById(did);
		dept.setDis(1);
		deptDao.update(dept);
	}

	@Override
	public Dept checkDept(String dname) {
		return deptDao.checkDept(dname);
	}

	@Override
	public List<Dept> findNoAllDept() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Dept.class);
		// 添加条件
		criteria.add(Restrictions.eq("dis", 1));
		return deptDao.findCriteria(criteria);
	}

}
